import request from '@/utils/request'

/* *********************************************************
 *
 * auth =====> 权限相关
 *
 ***********************************************************/
/**
 * 登录接口
 */
export function userLoginApi(data) {
  return request({
    url: '/auth/login',
    method: 'post',
    data
  })
}

/**
 * 获取用户信息
 */
export function getUserInfoApi(data) {
  return request({
    url: '/auth/info',
    method: 'get',
    data
  })
}

/**
 * 登录后根据token获取动态路由
 */
export function getUserRoutesApi() {
  return request({
    url: '/auth/routes',
    method: 'post'
  })
}

/**
 * 登录后根据token获取动态路由
 */
export function getUserBtnsApi() {
  return request({
    url: '/auth/btns',
    method: 'post'
  })
}

/**
 * 注销用户
 */
export function userLogoutApi() {
  return request({
    url: '/logout',
    method: 'post'
  })
}
/* *********************************************************
 *
 * user =====> 用户相关
 *
 ***********************************************************/
/**
 * 获取用户列表
 */
export function getUsersListApi() {
  return request({
    url: '/user/list',
    method: 'get'
  })
}

export function createUserApi(data) {
  return request({
    url: '/user/create',
    method: 'post',
    data
  })
}

export function updateUserApi(data) {
  return request({
    url: '/user/update/' + data._id,
    method: 'post',
    data
  })
}

export function deleteUserApi(data) {
  return request({
    url: '/user/remove/' + data._id,
    method: 'post'
  })
}
/* *********************************************************
 *
 * role =====> 角色相关
 *
 ***********************************************************/

export function getRoleListApi() {
  return request({
    url: '/roles',
    method: 'get'
  })
}

export function createRoleApi(data) {
  return request({
    url: '/role/create',
    method: 'post',
    data
  })
}

export function updateRoleApi(data) {
  return request({
    url: '/role/update/' + data._id,
    method: 'post',
    data
  })
}

export function removeRoleApi(data) {
  return request({
    url: '/role/delete/' + data._id,
    method: 'post'
  })
}

/**
 * 为角色分配菜单权限
 * @param {Object} data 菜单权限
 */
export function updateRoleAssignMenuPermsApi(data) {
  return request({
    data,
    url: '/role/assign/menuPerms',
    method: 'post'
  })
}

export function updateRoleAssignApiPermsApi(data) {
  return request({
    data,
    url: '/role/assign/apiPerms',
    method: 'post'
  })
}
/* *********************************************************
 *
 * menu =====> 菜单相关
 *
 ***********************************************************/
export function getMenuTreeIncludeBtnApi() {
  return request({
    method: 'get',
    url: '/menu/tree/btn'
  })
}

export function getMenuTreeNotIncludeBtnApi() {
  return request({
    method: 'get',
    url: '/menu/tree/noBtn'
  })
}

export function createMenuApi(data) {
  delete data._id
  delete data.id
  delete data.code
  return request({
    data,
    method: 'post',
    url: '/menu/create'
  })
}

export function createButtonApi(data) {
  const { title, type, code, parent } = data
  return request({
    data: { title, type, code, parent },
    method: 'post',
    url: '/button/create'
  })
}

export function updateMenuApi(data) {
  return request({
    method: 'post',
    url: '/menu/update',
    data
  })
}

export function removeMenuApi(id) {
  return request({
    method: 'post',
    url: `/menu/remove/${id}`
  })
}
/* *********************************************************
 *
 * api =====> api相关
 *
 ***********************************************************/
export function getApiListApi() {
  return request({
    url: '/api/tree',
    method: 'get'
  })
}

export function getApiTreeWithoutRequestApi() {
  return request({
    url: '/api/tree?q=group',
    method: 'get'
  })
}

// 创建api
export function createApiApi(data) {
  return request({
    url: '/api/create',
    method: 'post',
    data
  })
}

// 删除api
export function removeApiApi(data) {
  return request({
    url: '/api/remove/' + data._id,
    method: 'post'
  })
}

// 更新api
export function updateAPIApi(data) {
  return request({
    url: '/api/update/' + data._id,
    method: 'post',
    data
  })
}
